<?php


namespace app\common\model\system;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class ProductLog extends Model
{
    use SoftDelete;

    // 定义时间戳字段名
    protected $createTime = 'create_time';

    //库存修改明细
    public static function ProductSkuEdit($goodsid, $money, $type, $info, $user_id,$orderid)
    {
        if ($money != 0) {
            $goods = (new Product())->where('id', $goodsid)->find();
            if (empty($goods)) {
                return true;
            }
            $update = [
                'id' => $goods['id'],
                'stock' => $goods['stock'] + $money,
            ];
            $res = Product::update($update);
            $res2 = self::create([
                'goodsid' => $goodsid,
                'befores' => $goods['stock'],
                'money' => $money,
                'afters' => $update['stock'],
                'type' => $type,
                'info' => $info,
                'adminid' => session('AdminLogin.name') ?? 0,
                'user_id' => $user_id,
                'order_id' => $orderid
            ]);
            if (!$res || !$res2) {
                throw new \Exception("库存修改失败，请重试", 100);
            }
        }
    }
}
